package promotion

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"context"
)

// UpsertCouponItemCategoriesParam 设置促销活动商品类目配置
type SetPromotionItemCategoriesParam struct {
	PromotionPtr         *sqlc.Promotion
	ItemCategoryConfPtrs param.ItemCategoryConfigurationPtrArray
}

// SetPromotionItemCategories 设置促销活动商品类目配置
func (s *PromotionEntityServiceImp) SetPromotionItemCategories(ctx context.Context, q *sqlc.Queries, ptr *SetPromotionItemCategoriesParam) (err error) {
	promotionID := ptr.PromotionPtr.ID
	err = q.DisablePromotionItemCategoriesByPromotionID(ctx, promotionID)
	if err != nil {
		return
	}
	if len(ptr.ItemCategoryConfPtrs) > 0 {
		for idx, itemCategoryConf := range ptr.ItemCategoryConfPtrs {
			err = q.UpsertPromotionItemCategory(ctx, sqlc.UpsertPromotionItemCategoryParams{
				PromotionID:      promotionID,
				ItemCategoryID:   itemCategoryConf.ID,
				ItemCategoryName: itemCategoryConf.Name,
				SortNo:           int32(idx + 1),
			})
			if err != nil {
				return
			}
		}
	}
	return
}
